from django.db import models


# Create your models here.


class Author(models.Model):
    name = models.CharField('姓名',
                            db_index=True,
                            max_length=20)
    age = models.IntegerField('年龄', default=1)
    email = models.EmailField('邮箱', null=True)

    class Meta:
        verbose_name = '作者Author表'
        verbose_name_plural = '作者Author表'

    def __str__(self):
        return '姓名：%s,年龄%s' % (self.name, self.age)


class Wife(models.Model):
    """
        作家妻子模型
    """
    name = models.CharField("姓名", max_length=50)
    age = models.IntegerField("年龄", default=18)
    # 增加一对一属性
    author = models.OneToOneField(Author, on_delete=models.DO_NOTHING)

    def __str__(self):
        return '姓名:%s,年龄%s' % (self.name, self.age)


class Publisher(models.Model):
    name = models.CharField("出版社名称", max_length=50, unique=True)

    def __str__(self):
        return self.name


class Book(models.Model):
    title = models.CharField("书名1", max_length=50)
    price = models.DecimalField("定价", max_digits=7, decimal_places=2, default=0)
    pub_house = models.CharField("出版社", max_length=50, default="清华大学出版社")
    pub_date = models.DateField("出版时间", default='1970-1-1')
    market_price = models.DecimalField("市价", max_digits=7, decimal_places=2, default=9999)
    publisher = models.ForeignKey(Publisher, on_delete=models.DO_NOTHING, null=True)
    author = models.ManyToManyField(Author, null=True)

    def __str__(self):
        return "id: %d, 书名:%s  出版社: %s" % (
            self.id, self.title, self.pub_house
        )

    class Meta:
        # 该模型所用的数据表的名称。（设置完后需要立马更新同步数据库）
        # db_table = 'book_table'
        verbose_name = '图书Book表'
        verbose_name_plural = '图书Book表'
